找传奇、传世资源到传世资源站!

C#史上语句最少的:增删改查操作数据库(含数据库)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

只要几条语句轻松实现:增删改查操作数据库(含数据库)C#史上语句最少的:增删改查操作数据库(含数据库) C#数据库操作-第1张
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.OleDb;using System.Data.SqlClient;namespace test{ public partial class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private OleDbConnection oleConnection1 = null; private OleDbCommand oleCommand1 = null; public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();this.oleConnection1 = new OleDbConnection(test.database.dbConnection.connection);this.oleCommand1 = new OleDbCommand();this.oleCommand1.Connection = this.oleConnection1;//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//} private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“hyckDataSet.Item”中。您可以根据需要移动或删除它。 LoadData(); TxtNo.Focus(); dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.YellowGreen; } private void LoadData() { string sql = "select ItemNo AS 料号,ItemName AS 名称,ItemDesc AS 规格,QTY AS 数量,Unit AS 单位 from Item where 1=1 "; ; if (TxtNo.Text != "" ) { sql = sql " AND ItemNo like '%" TxtNo.Text "%'"; } if (TxtName.Text != "") { sql = sql " AND ItemName like '%" TxtName.Text "%'"; } if (TxtDesc.Text != "") { sql = sql " AND ItemDesc like '%" TxtDesc.Text "%'"; } if (TxtQty.Text != "") { sql = sql " AND QTY=" TxtQty.Text; } if (CbUnit.Text != "") { sql = sql " AND Unit='" CbUnit.Text "'"; } oleConnection1.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleConnection1); DataSet ds = new DataSet(); ds.Clear(); adp.Fill(ds, "Item"); dataGridView1.DataSource = ds.Tables[0].DefaultView; dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; oleConnection1.Close(); ClearText(); TxtNo.Focus(); } private void button1_Click(object sender, EventArgs e) { } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { UpdateData(); } private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 27) { this.Close(); } } private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { dataGridView1.CancelEdit(); e.Cancel = true; } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void button2_Click(object sender, EventArgs e) { } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 27) { this.Close(); } } private void UpdateData() { string sql=null; if (dataGridView1.CurrentCell.ColumnIndex == 1) sql = "update Item set ItemName='" dataGridView1.CurrentCell.Value.ToString() "' where ItemNo='" dataGridView1.CurrentRow.Cells[0].Value "'"; if (dataGridView1.CurrentCell.ColumnIndex == 2) sql = "update Item set ItemDesc='" dataGridView1.CurrentCell.Value.ToString() "' where ItemNo='" dataGridView1.CurrentRow.Cells[0].Value "'"; if (dataGridView1.CurrentCell.ColumnIndex == 3) sql = "update Item set QTY=" dataGridView1.CurrentCell.Value.ToString() " where ItemNo='" dataGridView1.CurrentRow.Cells[0].Value "'"; if (dataGridView1.CurrentCell.ColumnIndex == 4) sql = "update Item set Unit='" dataGridView1.CurrentCell.Value.ToString() "' where ItemNo='" dataGridView1.CurrentRow.Cells[0].Value "'"; if ( sql==null ) return; try { oleConnection1.Open(); oleCommand1.CommandText = sql; oleCommand1.ExecuteNonQuery(); oleConnection1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "出现错误"); } } private void button3_Click(object sender, EventArgs e) { } private void dataGridView1_KeyUp(object sender, KeyEventArgs e) { } private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { } private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { } private void BtnExit_Click(object sender, EventArgs e) { this.Close(); } private void BtnDel_Click(object sender, EventArgs e) { if (MessageBox.Show("确定要删除这行吗?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) return; if (dataGridView1.CurrentRow.Index >=0 && dataGridView1.DataSource != null ){ string sql = "delete * from Item where ItemNo='" dataGridView1.CurrentRow.Cells[0].Value "'";oleConnection1.Open();oleCommand1.CommandText = sql;oleCommand1.ExecuteNonQuery();oleConnection1.Close(); dataGridView1.Rows.Remove(dataGridView1.CurrentRow);}else {MessageBox.Show("没有指定物料信息!","提示"); } } private void BtnAdd_Click(object sender, EventArgs e) { TxtQty.Text = "0"; try { OleDbConnection oleDb = new OleDbConnection(test.database.dbConnection.connection); oleDb.Open(); string sql; if (TxtQty.Text == "") TxtQty.Text = "0"; if (TxtDesc.Text=="") { sql = "insert into Item (ItemNo,ItemName,ItemDesc,QTY,Unit) values ('" TxtNo.Text "','" TxtName.Text "',null," TxtQty.Text ",'" CbUnit.Text "')"; } else { sql = "insert into Item (ItemNo,ItemName,ItemDesc,QTY,Unit) values ('" TxtNo.Text "','" TxtName.Text "','" TxtDesc.Text "'," TxtQty.Text ",'" CbUnit.Text "')"; } OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDb); oleDbCommand.ExecuteNonQuery(); //返回被修改的数目 oleDb.Close(); MessageBox.Show("添加成功!"); ClearText(); dataGridView1.DataSource = null; LoadData(); } catch (Exception ex) { MessageBox.Show(ex.Message, "出现错误"); } } private void ClearText() { TxtNo.Text = ""; TxtName.Text = ""; TxtDesc.Text = ""; TxtQty.Text = ""; CbUnit.SelectedIndex = 0; TxtNo.Focus(); } private void BtnEdit_Click(object sender, EventArgs e) { dataGridView1.ReadOnly = false; dataGridView1.Columns[0].ReadOnly = true; } private void TxtQty_Enter(object sender, EventArgs e) { } private void TxtQty_Click(object sender, EventArgs e) { TxtQty.SelectAll(); } private void TxtQty_TextChanged(object sender, EventArgs e) { } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.F1: BtnAdd_Click(this, EventArgs.Empty); break; case Keys.F2: BtnDel_Click(this, EventArgs.Empty); break; case Keys.F3: BtnEdit_Click(this, EventArgs.Empty); break; case Keys.F4: BtnQry_Click(this, EventArgs.Empty); break; } } private void TxtQty_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && e.KeyChar != '.' && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { dataGridView1.ClearSelection(); } private void BtnQry_Click(object sender, EventArgs e) { LoadData(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复